import pydevd

import pandas
from absl import app, flags

from coin.base.datetime_util import (convert_string_to_dates, iterate_date)
from coin.exchange.bitfinex_v2.kr_rest.product import BitfinexProduct
from coin.exchange.huobi.kr_rest.product import HuobiProduct
from coin.exchange.okex.kr_rest.product import OkexProduct
from coin.experimental.xguo.order_and_feed_stats.logic.query_func import (read_csv_into_df,
                                                                          calculate_spread_bp)

FLAGS = flags.FLAGS


def main(argv):
  csv_root = FLAGS.csv_root
  assert csv_root, '--csv_root must be specified.'
  start_date = FLAGS.start_date
  assert start_date, '--start_date must be specified.'
  end_date = FLAGS.end_date
  assert end_date, '--end_date must be specified.'
  machine = FLAGS.machine
  assert machine, '--machine must be specified.'

  start_date = convert_string_to_dates(start_date)[0]
  end_date = convert_string_to_dates(end_date)[0]
  trading_dates = [td for td in iterate_date(start_date, end_date)]

  products = [
      BitfinexProduct.FromStr('ETC-USDT'),
      BitfinexProduct.FromStr('IOT-USDT'),
      BitfinexProduct.FromStr('NEO-USDT'),
      BitfinexProduct.FromStr('ETP-USDT'),
      HuobiProduct.FromStr('ETC-USDT'),
      HuobiProduct.FromStr('HT-USDT'),
      HuobiProduct.FromStr('IOST-USDT'),
      HuobiProduct.FromStr('ONT-USDT'),
      OkexProduct.FromStr('ETC-USDT'),
      OkexProduct.FromStr('WFEE-USDT'),
      OkexProduct.FromStr('OKB-USDT'),
      OkexProduct.FromStr('ONT-USDT'),
      OkexProduct.FromStr('MITH-USDT'),

      # BitfinexProduct.FromStr('BCH-USDT'),
      # BitfinexProduct.FromStr('BTC-USDT'),
      # BitfinexProduct.FromStr('EOS-USDT'),
      # BitfinexProduct.FromStr('ETH-USDT'),
      # BitfinexProduct.FromStr('LTC-USDT'),
      # BitfinexProduct.FromStr('XRP-USDT'),
      # HuobiProduct.FromStr('BCH-USDT'),
      # HuobiProduct.FromStr('BTC-USDT'),
      # HuobiProduct.FromStr('ETH-USDT'),
      # HuobiProduct.FromStr('EOS-USDT'),
      # HuobiProduct.FromStr('IOTA-USDT'),
      # HuobiProduct.FromStr('LTC-USDT'),
      # HuobiProduct.FromStr('XRP-USDT'),
      # OkexProduct.FromStr('BCH-USDT'),
      # OkexProduct.FromStr('BTC-USDT'),
      # OkexProduct.FromStr('EOS-USDT'),
      # OkexProduct.FromStr('ETH-USDT'),
      # OkexProduct.FromStr('IOTA-USDT'),
      # OkexProduct.FromStr('LTC-USDT'),
      # OkexProduct.FromStr('XRP-USDT'),
  ]

  interval_sec = 1200
  correlation_matrix = []
  for trading_date in trading_dates:
    correlation_list = []
    for product in products:
      df = read_csv_into_df(csv_root, machine, trading_date, product.exchange, product.symbol)
      volatility = calculate_volatility(df, interval_sec)
      true_book_spread = calculate_true_book_spread(df, interval_sec)
      correlation = volatility.corr(true_book_spread)
      correlation_list.append(correlation)
    correlation_matrix.append(correlation_list)
  df = pandas.DataFrame(correlation_matrix, columns=products, index=trading_dates)
  df = df.T
  formatter = lambda x: '%.2f' % x
  df = df.applymap(formatter)
  print(df.to_string())

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_root', None, 'Output csv files root directory.')

  flags.DEFINE_string('start_date', None, 'yyyymmdd')

  flags.DEFINE_string('end_date', None, 'yyyymmdd. Feed at end_date is not checked.')

  flags.DEFINE_string('machine', None, 'Instance machine name.')

  app.run(main)
